<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>项目管理 Dashboard</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body class="bg-gray-50">
    <!-- 导航栏 -->
    <nav class="bg-white shadow-sm border-b">
        <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
            <div class="flex justify-between h-16">
                <div class="flex items-center">
                    <h1 class="text-xl font-semibold text-gray-900">项目管理工具</h1>
                </div>
                <div class="flex items-center space-x-4">
                    <button id="createProjectBtn" class="bg-blue-600 hover:bg-blue-700 text-white px-4 py-2 rounded-lg text-sm font-medium">
                        创建项目
                    </button>
                    <button id="logoutBtn" class="text-gray-500 hover:text-gray-700 text-sm">
                        退出登录
                    </button>
                </div>
            </div>
        </div>
    </nav>

    <!-- 主要内容 -->
    <main class="max-w-7xl mx-auto py-6 px-4 sm:px-6 lg:px-8">
        <div class="mb-6">
            <h2 class="text-2xl font-bold text-gray-900">项目概览</h2>
            <p class="text-gray-600">管理和跟踪您的项目进度</p>
        </div>

        <!-- 项目网格 -->
        <div id="projectsGrid" class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-6">
            <!-- 项目卡片将通过JavaScript动态加载 -->
        </div>

        <!-- 空状态 -->
        <div id="emptyState" class="text-center py-12 hidden">
            <div class="mx-auto h-12 w-12 text-gray-400">
                <svg fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 11H5m14 0a2 2 0 012 2v6a2 2 0 01-2 2H5a2 2 0 01-2-2v-6a2 2 0 012-2m14 0V9a2 2 0 00-2-2M5 11V9a2 2 0 012-2m0 0V5a2 2 0 012-2h6a2 2 0 012 2v2M7 7h10"></path>
                </svg>
            </div>
            <h3 class="mt-2 text-sm font-medium text-gray-900">暂无项目</h3>
            <p class="mt-1 text-sm text-gray-500">开始创建您的第一个项目</p>
            <div class="mt-6">
                <button id="createFirstProjectBtn" class="inline-flex items-center px-4 py-2 border border-transparent shadow-sm text-sm font-medium rounded-md text-white bg-blue-600 hover:bg-blue-700">
                    创建项目
                </button>
            </div>
        </div>
    </main>

    <!-- 创建项目模态框 -->
    <div id="createProjectModal" class="fixed inset-0 bg-gray-600 bg-opacity-50 overflow-y-auto h-full w-full hidden">
        <div class="relative top-20 mx-auto p-5 border w-96 shadow-lg rounded-md bg-white">
            <div class="mt-3">
                <div class="flex items-center justify-between mb-4">
                    <h3 class="text-lg font-medium text-gray-900">创建新项目</h3>
                    <button id="closeModalBtn" class="text-gray-400 hover:text-gray-600">
                        <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"></path>
                        </svg>
                    </button>
                </div>
                <form id="createProjectForm">
                    <div class="mb-4">
                        <label for="projectName" class="block text-sm font-medium text-gray-700 mb-2">项目名称</label>
                        <input type="text" id="projectName" name="name" required 
                               class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500">
                    </div>
                    <div class="mb-6">
                        <label for="projectDescription" class="block text-sm font-medium text-gray-700 mb-2">项目描述</label>
                        <textarea id="projectDescription" name="description" rows="3"
                                  class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500"></textarea>
                    </div>
                    <div class="flex justify-end space-x-3">
                        <button type="button" id="cancelBtn" class="px-4 py-2 text-sm font-medium text-gray-700 bg-gray-100 hover:bg-gray-200 rounded-md">
                            取消
                        </button>
                        <button type="submit" class="px-4 py-2 text-sm font-medium text-white bg-blue-600 hover:bg-blue-700 rounded-md">
                            创建
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <script>
        $(document).ready(function() {
            loadProjects();
            
            // 创建项目按钮事件
            $('#createProjectBtn, #createFirstProjectBtn').click(function() {
                $('#createProjectModal').removeClass('hidden');
            });
            
            // 关闭模态框
            $('#closeModalBtn, #cancelBtn').click(function() {
                $('#createProjectModal').addClass('hidden');
                $('#createProjectForm')[0].reset();
            });
            
            // 创建项目表单提交
            $('#createProjectForm').submit(function(e) {
                e.preventDefault();
                
                const formData = {
                    name: $('#projectName').val(),
                    description: $('#projectDescription').val()
                };
                
                $.ajax({
                    url: '/api/projects',
                    method: 'POST',
                    contentType: 'application/json',
                    data: JSON.stringify(formData),
                    success: function(response) {
                        $('#createProjectModal').addClass('hidden');
                        $('#createProjectForm')[0].reset();
                        loadProjects();
                        showNotification('项目创建成功！', 'success');
                    },
                    error: function(xhr) {
                        const error = xhr.responseJSON?.error || '创建项目失败';
                        showNotification(error, 'error');
                    }
                });
            });
            
            // 退出登录
            $('#logoutBtn').click(function() {
                $.ajax({
                    url: '/api/logout',
                    method: 'POST',
                    success: function() {
                        window.location.href = '/login';
                    }
                });
            });
        });
        
        function loadProjects() {
            $.ajax({
                url: '/api/projects',
                method: 'GET',
                success: function(response) {
                    const projects = response.projects;
                    const grid = $('#projectsGrid');
                    const emptyState = $('#emptyState');
                    
                    if (projects.length === 0) {
                        grid.addClass('hidden');
                        emptyState.removeClass('hidden');
                    } else {
                        grid.removeClass('hidden');
                        emptyState.addClass('hidden');
                        
                        grid.empty();
                        projects.forEach(function(project) {
                            const projectCard = createProjectCard(project);
                            grid.append(projectCard);
                        });
                    }
                },
                error: function(xhr) {
                    console.error('Failed to load projects:', xhr);
                    showNotification('加载项目失败', 'error');
                }
            });
        }
        
        function createProjectCard(project) {
            const totalItems = project.task_count + project.issue_count + project.requirement_count;
            const progress = totalItems > 0 ? Math.round((project.task_count / totalItems) * 100) : 0;
            
            return $(`
                <div class="bg-white rounded-lg shadow-sm border hover:shadow-md transition-shadow cursor-pointer project-card" data-project-id="${project.id}">
                    <div class="p-6">
                        <div class="flex items-center justify-between mb-4">
                            <h3 class="text-lg font-medium text-gray-900 truncate">${project.name}</h3>
                            <div class="flex space-x-2">
                                <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-green-100 text-green-800">
                                    ${project.task_count} 任务
                                </span>
                                <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-red-100 text-red-800">
                                    ${project.issue_count} 问题
                                </span>
                            </div>
                        </div>
                        
                        <p class="text-gray-600 text-sm mb-4 line-clamp-2">${project.description || '暂无描述'}</p>
                        
                        <div class="mb-4">
                            <div class="flex justify-between text-sm text-gray-600 mb-1">
                                <span>进度</span>
                                <span>${progress}%</span>
                            </div>
                            <div class="w-full bg-gray-200 rounded-full h-2">
                                <div class="bg-blue-600 h-2 rounded-full" style="width: ${progress}%"></div>
                            </div>
                        </div>
                        
                        <div class="text-xs text-gray-500">
                            创建于 ${new Date(project.created_at).toLocaleDateString()}
                        </div>
                    </div>
                </div>
            `);
        }
        
        // 项目卡片点击事件
        $(document).on('click', '.project-card', function() {
            const projectId = $(this).data('project-id');
            window.location.href = `/projects/${projectId}`;
        });
        
        function showNotification(message, type) {
            const bgColor = type === 'success' ? 'bg-green-500' : 'bg-red-500';
            const notification = $(`
                <div class="fixed top-4 right-4 ${bgColor} text-white px-6 py-3 rounded-lg shadow-lg z-50">
                    ${message}
                </div>
            `);
            
            $('body').append(notification);
            
            setTimeout(function() {
                notification.fadeOut(function() {
                    notification.remove();
                });
            }, 3000);
        }
    </script>
</body>
</html>
